package com.manager.cms.controller;

import com.manager.cms.annotation.IgnoreAuth;
import com.manager.cms.annotation.IgnoreLogin;
import com.manager.cms.bo.SysAdmin;
import com.manager.cms.bo.UserRole;
import com.manager.cms.common.CommonResponse;
import com.manager.cms.common.Pageable;
import com.manager.cms.service.SysAdminService;
import com.manager.cms.service.UserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author
 * @version v1.0
 * @date 2020-05-19
 */
@Slf4j
@RestController
@RequestMapping(value = "/sysAdmin")
public class SysAdminController {

    @Autowired
    private SysAdminService sysadminservice;

    @Autowired
    private UserRoleService userroleservice;

    @IgnoreAuth
    @IgnoreLogin
    @PostMapping("/register")
    public CommonResponse register(HttpServletRequest request, HttpServletResponse response, SysAdmin record) {
        try {
            SysAdmin query = new SysAdmin();
            query.setUsername(record.getUsername());
            List<SysAdmin> list = sysadminservice.getList(query);
            if (list.size() > 0) {
                return CommonResponse.fail("账号已存在");
            }
            record.setId(UUID.randomUUID().toString());
            record.setModifyDate(new Date());
            record.setCreateDate(new Date());
            sysadminservice.insert(record);

            UserRole userRole = new UserRole();
            userRole.setId(UUID.randomUUID().toString());
            userRole.setUserId(record.getId());
            userRole.setRole("admin");
            userroleservice.insert(userRole);
            return CommonResponse.succeed();
        } catch (Exception e) {
            log.error("保存失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @PostMapping("/save")
    public CommonResponse save(HttpServletRequest request, HttpServletResponse response, SysAdmin record) {
        try {
            if (record.getId() == null) {

                SysAdmin query = new SysAdmin();
                query.setUsername(record.getUsername());
                List<SysAdmin> list = sysadminservice.getList(query);
                if (list.size() > 0) {
                    return CommonResponse.fail("账号已存在");
                }

                record.setId(UUID.randomUUID().toString());
                record.setModifyDate(new Date());
                record.setCreateDate(new Date());
                sysadminservice.insert(record);
                UserRole userRole = new UserRole();
                userRole.setId(UUID.randomUUID().toString());
                userRole.setUserId(record.getId());
                userRole.setRole("admin");
                userroleservice.insert(userRole);
            } else {
                SysAdmin sysAdmin = sysadminservice.selectByPrimaryKey(record.getId());
                sysAdmin.setModifyDate(new Date());
                sysAdmin.setMobile(record.getMobile());
                sysAdmin.setEmail(record.getEmail());
                sysAdmin.setUsername(record.getUsername());
                sysAdmin.setName(record.getName());
                sysAdmin.setPassword(record.getPassword());
                sysAdmin.setMemo(record.getMemo());
                sysadminservice.update(sysAdmin);
            }
            return CommonResponse.succeed();
        } catch (Exception e) {
            log.error("保存失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @PostMapping("/delete")
    public CommonResponse del(HttpServletRequest request, HttpServletResponse response, String id) {
        try {
            if (id == null) {
                return CommonResponse.fail("id不能为空");
            }
            sysadminservice.del(id);
            return CommonResponse.succeed();
        } catch (Exception e) {
            log.error("删除失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @GetMapping("/selectByPrimaryKey")
    public CommonResponse selectByPrimaryKey(HttpServletRequest request, HttpServletResponse response, String id) {
        try {
            if (id == null) {
                return CommonResponse.fail("id不能为空");
            }
            SysAdmin info = sysadminservice.selectByPrimaryKey(id);
            return CommonResponse.succeed(info);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @GetMapping("/list")
    public CommonResponse getList(HttpServletRequest request, HttpServletResponse response, SysAdmin record) {
        try {
            List<SysAdmin> list = sysadminservice.getList(record);
            return CommonResponse.succeed(list);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @GetMapping("/pageList")
    public CommonResponse pageList(HttpServletRequest request, HttpServletResponse response, SysAdmin record,
                                   @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        try {
            Pageable<SysAdmin> page = sysadminservice.pageList(record, pageNo, pageSize);
            return CommonResponse.succeed(page);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }
}